#
# Warning:
# Do not run this script stand-alone.
# It will be invoked by 'oms/tools/config_via_dialog.sh'.
#

# --------------------------------------------------
# --------------------- LDAP -----------------------
# --------------------------------------------------

# LDAP suffix.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "LDAP suffix" \
    --inputbox "\
Please specify your LDAP suffix.

EXAMPLE:

    +---------------------+-----------------------+
    | Your domain name    | Recommend LDAP suffix |
    +---------------------+-----------------------+
    | openbsdonly.org     | dc=openbsdonly,dc=org |
    +---------------------+-----------------------+
    | abcde.com.cn        | dc=abcde,dc=com.cn    |
    +---------------------+-----------------------+

" \
    20 76 "dc=openbsdonly,dc=org" \
    2>/tmp/ldap_suffix

LDAP_SUFFIX="$(cat /tmp/ldap_suffix)"
LDAP_SUFFIX_MAJOR="$(sed 's/dc=//g' /tmp/ldap_suffix | awk -F',' '{print $1}')"
LDAP_SUFFIX_MINOR="$(sed 's/dc=//g' /tmp/ldap_suffix | awk -F',' '{print $2}')"
LDAP_BINDDN="cn=${VMAIL_USER_NAME},${LDAP_SUFFIX}"
LDAP_ADMIN_DN="cn=${VMAIL_ADMIN_USER_NAME},${LDAP_SUFFIX}"
LDAP_ROOTDN="cn=Manager,${LDAP_SUFFIX}"
LDAP_BASEDN_NAME='domains'
LDAP_BASEDN="o=${LDAP_BASEDN_NAME},${LDAP_SUFFIX}"
rm -f /tmp/ldap_suffix

cat >> ${CONFIG_FILE} <<EOF
LDAP_SUFFIX="${LDAP_SUFFIX}"
LDAP_SUFFIX_MAJOR="${LDAP_SUFFIX_MAJOR}"
LDAP_SUFFIX_MINOR="${LDAP_SUFFIX_MINOR}"
LDAP_BINDDN="cn=${VMAIL_USER_NAME},${LDAP_SUFFIX}"
LDAP_ADMIN_DN="${LDAP_ADMIN_DN}"
LDAP_ROOTDN="cn=Manager,${LDAP_SUFFIX}"
LDAP_BASEDN_NAME="domains"
LDAP_BASEDN="o=${LDAP_BASEDN_NAME},${LDAP_SUFFIX}"
EOF

# LDAP rootpw.
while : ; do
    ${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Password for LDAP rootdn: ${LDAP_ROOTDN}" \
    --inputbox "\
Please specify password for LDAP 'rootdn':

    * ${LDAP_ROOTDN}

Warning:

    * EMPTY password is *NOT* permit.
" \
    20 76 \
    2>/tmp/ldap_rootpw

    LDAP_ROOTPW="$(cat /tmp/ldap_rootpw)"
    if [ ! -z "${LDAP_ROOTPW}" ]; then
        break
    fi
done

echo "LDAP_ROOTPW='${LDAP_ROOTPW}'" >>${CONFIG_FILE}
rm /tmp/ldap_rootpw

# VMAIL USER PASSWORD
while : ; do
    ${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Password for LDAP bind dn and admin dn" \
    --inputbox "\
Please specify password for LDAP bind dn and admin dn:

    * bind dn:  ${LDAP_BINDDN}
    * admin dn: ${LDAP_ADMIN_DN}

Warning:

    * EMPTY password is *NOT* permit.

" \
    20 76 \
    2>/tmp/vmail_user_passwd

    LDAP_BINDPW="$(cat /tmp/vmail_user_passwd)"
    LDAP_ADMIN_PW="${LDAP_BINDPW}"
    if [ ! -z "${LDAP_BINDPW}" ]; then
        break
    fi
done

echo "LDAP_BINDPW='${LDAP_BINDPW}'" >>${CONFIG_FILE}
echo "LDAP_ADMIN_PW='${LDAP_BINDPW}'" >>${CONFIG_FILE}
rm -f /tmp/vmail_user_passwd

# INFO
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "LDAP bind and manage info" \
    --msgbox "\
Important Note:

    * Bind info for all LDAP operations:

        * LDAP server:  ${LDAP_SERVER_HOST}
        * LDAP port:    ${LDAP_SERVER_PORT}
        * LDAP version: ${LDAP_BIND_VERSION}
        * Bind dn:      ${LDAP_BINDDN}
        * Bind passwdw: ${LDAP_BINDPW}

    * All LDAP attributions can be managed by:

        * LDAP dn: ${LDAP_ADMIN_DN}
        * LDAP pw: ${LDAP_ADMIN_PW}

    * phpLDAPadmin: http://$(hostname)/phpldapadmin/
" \
    20 76 

# Your domain name.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Your first virtual domain" \
    --inputbox "\
Please specify the name of your first virtual domain.

EXAMPLE:

    * ${LDAP_SUFFIX_MAJOR}.${LDAP_SUFFIX_MINOR}

The LDAP dn of your domain will be:

    * o=${LDAP_SUFFIX_MAJOR}.${LDAP_SUFFIX_MINOR},${LDAP_BASEDN}
" \
    20 76 "${LDAP_SUFFIX_MAJOR}.${LDAP_SUFFIX_MINOR}" \
    2>/tmp/ldap_your_domain

FIRST_DOMAIN="$(cat /tmp/ldap_your_domain)"
echo "FIRST_DOMAIN='${FIRST_DOMAIN}'" >>${CONFIG_FILE}
rm -f /tmp/ldap_your_domain

#FIRST_DOMAIN_ADMIN_NAME
FIRST_DOMAIN_ADMIN_NAME="postmaster"
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Specify administrator' name of your virtual domain" \
    --inputbox "\
Please specify administrator' name of your virtual domain.

EXAMPLE:

    * ${FIRST_DOMAIN_ADMIN_NAME}

The full mail address and LDAP dn of the user will be:

    * ${FIRST_DOMAIN_ADMIN_NAME}@${FIRST_DOMAIN}
    * mail=${FIRST_DOMAIN_ADMIN_NAME}@${FIRST_DOMAIN}, \\
      o=${FIRST_DOMAIN}, \\
      ${LDAP_BASEDN}
" \
    20 76 "${FIRST_DOMAIN_ADMIN_NAME}" \
    2>/tmp/FIRST_DOMAIN_ADMIN_NAME

FIRST_DOMAIN_ADMIN_NAME="$(cat /tmp/FIRST_DOMAIN_ADMIN_NAME)"
echo "FIRST_DOMAIN_ADMIN_NAME='${FIRST_DOMAIN_ADMIN_NAME}'" >>${CONFIG_FILE}
rm -f /tmp/FIRST_DOMAIN_ADMIN_NAME

# FIRST_DOMAIN_ADMIN_PASSWD
while : ; do
    ${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Password for the administrator of your domain" \
    --inputbox "\
Please specify password for the administrator user:

    * ${FIRST_DOMAIN_ADMIN_NAME}@${FIRST_DOMAIN}

Warning:

    * EMPTY password is *NOT* permit.

" \
    20 76 \
    2>/tmp/FIRST_DOMAIN_ADMIN_PASSWD

    FIRST_DOMAIN_ADMIN_PASSWD="$(cat /tmp/FIRST_DOMAIN_ADMIN_PASSWD)"
    if [ ! -z "${FIRST_DOMAIN_ADMIN_PASSWD}" ]; then
        break
    fi
done

echo "FIRST_DOMAIN_ADMIN_PASSWD='${FIRST_DOMAIN_ADMIN_PASSWD}'" >>${CONFIG_FILE}
rm -f /tmp/FIRST_DOMAIN_ADMIN_PASSWD

#FIRST_USER
FIRST_USER='www'
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Add a example user for your virtual domain" \
    --inputbox "\
Please specify the username of your first virtual domain: ${FIRST_DOMAIN}.

EXAMPLE:

    * ${FIRST_USER}

The full mail address of the user will be:

    * ${FIRST_USER}@${FIRST_DOMAIN}
" \
    20 76 "${FIRST_USER}" \
    2>/tmp/FIRST_USER

FIRST_USER="$(cat /tmp/FIRST_USER)"
echo "FIRST_USER='${FIRST_USER}'" >>${CONFIG_FILE}
rm -f /tmp/FIRST_USER

# FIRST_USER_PASSWD
while : ; do
    ${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Password for your example user" \
    --inputbox "\
Please specify password for your example user:

    * ${FIRST_USER}@${FIRST_DOMAIN}

Warning:

    * EMPTY password is *NOT* permit.

" \
    20 76 \
    2>/tmp/FIRST_USER_PASSWD

    FIRST_USER_PASSWD="$(cat /tmp/FIRST_USER_PASSWD)"
    if [ ! -z "${FIRST_USER_PASSWD}" ]; then
        break
    fi
done

echo "FIRST_USER_PASSWD='${FIRST_USER_PASSWD}'" >>${CONFIG_FILE}
rm -f /tmp/FIRST_USER_PASSWD

